package com.jiaanpei.frameproject.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jiaanpei.frameproject.R;
import com.jiaanpei.frameproject.modle.appupdate.UpdateEvent;
import com.jiaanpei.frameproject.modle.bean.appupdate.AppUpdateBean;
import com.jiaanpei.frameproject.utils.ScreenUtil;
import com.jiaanpei.frameproject.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyAPPUpdateDialog extends Dialog {
    private Context context;
    private LayoutInflater inflater;
    LinearLayout view;
    AppUpdateBean bean;

    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.content)
    TextView content;

    @BindView(R.id.cancel)
    Button cancel;
    @BindView(R.id.submit)
    Button submit;

    @BindView(R.id.pb)
    public ProgressBar pb;

    public MyAPPUpdateDialog(Context context,AppUpdateBean bean) {
        super(context, R.style.WaitingDialog);

        this.context=context;
        this.bean=bean;
    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();
    }



    private void initView() {
        inflater=getLayoutInflater();
        view=(LinearLayout)inflater.inflate(R.layout.ac_appdownload_layout, null);
        setContentView(view);
        ButterKnife.bind(this, view);
        version.setText(new StringBuilder().append("最新版本V").append(bean.getNewVersion()).append(":").toString());
        content.setText(bean.getUpdateDescription());



    }

    @OnClick({R.id.submit,R.id.cancel})
    public void black(View view){
        switch (view.getId()){
            case R.id.submit:
                if(!TextUtils.isEmpty(bean.getApkUrl())){
                    pb.setVisibility(View.VISIBLE);
                    submit.setEnabled(false);
                    cancel.setEnabled(false);
                    EventBus.getDefault().post(new UpdateEvent(bean.getApkUrl()));
                }else{
                    ToastUtil.show(context,content.getResources().getString(R.string.dataerror));
                }
                break;
            case R.id.cancel:
                dismiss();
                break;

        }

    }

    @Override
    public void show() {
        super.show();
        Window dialogWindow = this.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ScreenUtil.dp2px(context,300);
        lp.height= ViewGroup.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        pb.setVisibility(View.INVISIBLE);
        pb.setMax(100);
        pb.setProgress(0);
        submit.setEnabled(true);
        String forceUpdate = bean.getForceUpdate();
        if(!TextUtils.isEmpty(forceUpdate)&&forceUpdate.equals("1")){
            cancel.setVisibility(View.GONE);
            this.setCancelable(false);
        }else{
            cancel.setVisibility(View.VISIBLE);
            this.setCancelable(true);
        }
    }
}
